package com.gmt;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

/**
 * @author John.zhang
 * @version Id: GmDefineGatewayFactory, v 1.0 2020/11/20 17:06 ZhiYong Exp $
 */

@Component
public class GmDefineGatewayFilterFactory extends AbstractGatewayFilterFactory<GmDefineGatewayFilterFactory.Config> {

    Logger logger = LoggerFactory.getLogger(GmDefineGatewayFilterFactory.class);

    public GmDefineGatewayFilterFactory() {
        super(Config.class);
    }


    public static final String NAME_KEY = "name";

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList(NAME_KEY);
    }

    @Override
    public GatewayFilter apply(Config config) {
        return ((exchange, chain) -> {
            logger.info("[Pre]GmDefineGatewayFilterFactory.apply"+config.getName());
            return chain.filter(exchange).then(Mono.fromRunnable(()->{
                exchange.getResponse().getStatusCode();
                logger.info("[Post]GmDefineGatewayFilterFactory.apply"+config.getName());
            }));
        });
    }


    public static class Config {

        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

}
